/*
 * FileStorageTester.java
 * JMUnit based test
 *
 * Created on 3-Dec-2009, 2:12:18 AM
 */

package ocbn.remotelab.test_package;


import ocbn.remotelab.events.*;
import jmunit.framework.cldc10.*;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.Connector;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Vector;

/**
 * @author Ke
 */
public class FileStorageTester extends TestCase {
    String testWrong;//gives a wrong directory, no file is created
    String testWriteFile;//a location to write into a mock file
    String EmptyFile;//an empty text file.
    FileConnection fc;

    public void setUp(){
        testWrong = "file:///SDCard/BlackBerry/Wrong.txt";

       //use this directory to see if the method can properly write to this file
       testWriteFile = "file:///SDCard/BlackBerry/TestWrite.txt";
       EmptyFile = "file:///SDCard/BlackBerry/Empty.txt";


   }

    public void tearDown(){
        /**
         * Delete all the created files for this test run.
         *
         */
         try{
                fc = (FileConnection)Connector.open(EmptyFile);
                if(fc.exists()){
                fc.delete();
                fc.close();
                }
             } catch (IOException ex){
                 System.out.println(ex.getMessage());
                 fail();

            }

       try{
                fc = (FileConnection)Connector.open(testWriteFile);
                if(fc.exists()){
                fc.delete();
                fc.close();
                }
             } catch (IOException ex){
                 System.out.println(ex.getMessage());
                 fail();

            }

    }

    /**
     * Consctructor for the tester.
     */
    public FileStorageTester() {
        //The first parameter of inherited constructor is the number of test cases
        super(6,"FileStorageTester");
    }

    /**
     * Runs each test cases
     * @param testNumber the number of tests to run
     * @throws Throwable
     */
    public void test(int testNumber) throws Throwable {
       switch(testNumber){
        case 0:
            testWrongFile();
            break;
        case 1:
            testAppendStringFileExists();
            break;
        case 2:
            testLoadEmptyFile();
            break;
        case 3:
             testAppendandRead();
             break;
        case 4:
             testAppendandReadTwoEvents();
             break;
        case 5:
             testClearMSEvents();
             break;
        default:
           break;
        }
    }


     /**
      * Test if a wrong file is properly detected by this tester.
      */
     public void testWrongFile()throws AssertionFailedException {

         try{
          //open up an empty file since no write method has been called
         fc = (FileConnection)Connector.open(testWrong);
            assertFalse(fc.exists());
         } catch (IOException ex) {
             fail();
         }

     }

     /**
      * Append an empty string and see if it properly creates a file.
      * @throws AssertionFailedException
      */
     public void testAppendStringFileExists() throws AssertionFailedException {
         //write a simple String to the file.
         FileStorage.appendXML(testWriteFile, "Test");
         try{
         fc = (FileConnection)Connector.open(testWriteFile);
         //check if fileExists.
         assertTrue(fc.exists());

         } catch (IOException ex) {
             fail();
             System.out.println(ex.getMessage());
         }
     }


    /**
     * Manually create an empty txt file and read it.
     * @throws AssertionFailedException
     */
     public void testLoadEmptyFile()throws AssertionFailedException{

         //create an empty txt file.
          try{
                fc = (FileConnection)Connector.open(EmptyFile);
                //create the file
                if(!fc.exists()){
                fc.create();
                fc.close();
                }
             } catch (IOException ex){
                 //if there's a problem with file this test fails.
                fail();
               System.out.println(ex.getMessage());
            }


      //Read the empty file.
      Vector events = FileStorage.loadMSEvents(EmptyFile);

       //the vector of events should be empty .
       assertTrue(events.isEmpty());
      
     }

   
    /**
     * Test writing one MS event to the file and read it.
     * @throws AssertionFailedException
     */
     public void testAppendandRead() throws AssertionFailedException {

         //create a String for the event and write it.
        String s =
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<data>" +
                    "<event type=\"MS State\">" +
                        "<id>0x0001</id>" +
                        "<time>Oct 3, 2009 10:12:25 AM</time>" +
                        "<state>failed</state>" +
                        "<source>MS #1</source>" +
                        "<details>" +
                            "<strong>Analyst terminated.</strong><br />" +
                            "Error code: 0x0000036" +
                        "</details>" +
                    "</event>" +
                "</data>";
         FileStorage.appendXML(testWriteFile,s);

         //Read the event file.
         Vector events = FileStorage.loadMSEvents(testWriteFile);
         Event e = (Event) events.elementAt(0);

         //the vector should contain one MS event object.
        assertEquals(e.getType(),MassSpectrometerStateEvent.class);
     }

    /**
     * Test writing two MS event to the file and read it.
     * @throws AssertionFailedException
     */
     public void testAppendandReadTwoEvents() throws AssertionFailedException {

         //create a String for the event and write it.
        String s =
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<data>" +
                    "<event type=\"MS State\">" +
                        "<id>0x0001</id>" +
                        "<time>Oct 3, 2009 10:12:25 AM</time>" +
                        "<state>failed</state>" +
                        "<source>MS #1</source>" +
                        "<details>" +
                            "<strong>Analyst terminated.</strong><br />" +
                            "Error code: 0x0000036" +
                        "</details>" +
                    "</event>" +
                "</data>";
        //write two events into the file.
         FileStorage.appendXML(testWriteFile,s);
         FileStorage.appendXML(testWriteFile,s);

         //Read the event file.
         Vector events = FileStorage.loadMSEvents(testWriteFile);
         Event e = (Event) events.elementAt(0);
         Event e2 = (Event) events.elementAt(1);

         //the vector should contain two MS event objects.
        assertEquals(e.getType(),MassSpectrometerStateEvent.class);
        assertEquals(e2.getType(),MassSpectrometerStateEvent.class);
     }


     /**
      * Test to see if this deletes the file contents properly.
      * @throws AssertionFailedException
      */
     public void testClearMSEvents()throws AssertionFailedException{
           //create a String for the event and write it.
        String s =
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<data>" +
                    "<event type=\"MS State\">" +
                        "<id>0x0001</id>" +
                        "<time>Oct 3, 2009 10:12:25 AM</time>" +
                        "<state>failed</state>" +
                        "<source>MS #1</source>" +
                        "<details>" +
                            "<strong>Analyst terminated.</strong><br />" +
                            "Error code: 0x0000036" +
                        "</details>" +
                    "</event>" +
                "</data>";
        //write the event into the file.
         FileStorage.appendXML(testWriteFile,s);
         FileStorage.clearMSEvent(testWriteFile);

         //Try reading to see if the file is empty.
        Vector events = FileStorage.loadMSEvents(testWriteFile);

         //the vector of events should be empty .
         assertTrue(events.isEmpty());
     }

}

